package com.example.client.controller;

import com.example.client.domain.Order;
import com.example.client.domain.Product;
import com.example.client.pojo.Result;
import com.example.client.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/client/products")
public class ProductController {

    @Autowired
    private ProductService productService;


    @Autowired
    private HttpSession httpSession;

    @GetMapping
    @PreAuthorize("hasAuthority('test')")
    public Result<List<Product>> selectProducts() {
        return productService.getProducts();
    }

    @PostMapping("/order")
    public Result order(Order order) {
        String username = (String) httpSession.getAttribute("username");
        order.setUsername(username);
        return productService.order(order);
    }

    @GetMapping("/shoppingCart")
    public Result<List<Order>>  shoppingCart() {
        String username = (String) httpSession.getAttribute("username");
        return productService.shoppingCart(username);
    }
}
